package com.feasy.generator.generatorcode.action;

import com.feasy.generator.generatorcode.service.GeneratorCodeService;
import com.feasy.generator.managedb.utils.DataBaseUtils;
import com.feasy.generator.managedb.utils.LayUiResult;
import com.feasy.generator.managedb.utils.ResponseUtils;
import com.feasy.generator.managedb.utils.StringUtils;
import io.github.yidasanqian.utils.JsonUtil;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/generatorCodeAction")
public class GeneratorCodeAction {

    @Autowired
    private GeneratorCodeService generatorCodeService;

    @CrossOrigin
    @RequestMapping("generatorCode")
    public void generatorCode(HttpServletRequest request, HttpServletResponse response) {
        ResponseUtils responseUtils = new ResponseUtils();
        try {
            String dbtype = request.getParameter("dbtype");
            String ip = request.getParameter("ip");
            String port = request.getParameter("port");
            String dbname = request.getParameter("dbname");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String tableName = request.getParameter("tableName");
            String packagePath = request.getParameter("packagePath");
            String author = request.getParameter("author");
            String email = request.getParameter("email");

            if (StringUtils.isEmpty(ip) || StringUtils.isEmpty(port) || StringUtils.isEmpty(dbname) || StringUtils.isEmpty(username)
                    || StringUtils.isEmpty(password) || StringUtils.isEmpty(tableName) || StringUtils.isEmpty(packagePath) || StringUtils.isEmpty(author)
                    || StringUtils.isEmpty(email) || StringUtils.isEmpty(dbtype)) {
                throw new RuntimeException("不允许有空参传入");
            }

            byte[] data = generatorCodeService.generatorCode(dbtype, ip, port, dbname, username, password, tableName, packagePath, author, email);

            response.reset();
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Content-Disposition", "attachment; filename=\"justdo.zip\"");
            response.addHeader("Content-Length", "" + data.length);
            response.setContentType("application/octet-stream; charset=UTF-8");
            IOUtils.write(data, response.getOutputStream());

        }catch (RuntimeException | IOException e) {
            e.printStackTrace();
            responseUtils.setStatus("0");
            responseUtils.setMessage(e.getMessage());
        }

    }

}
